// manager/pages/merchantmember/index.js
import config from '../../../utils/config';
import { request } from '../../../utils/request';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        isCalculation: config.isCalculation,
        // 商户id
        merchantId: "",
        // 1-个人，2-商户，3-企业，4-新型农业主体
        merchantType: 2,
        memberList: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            merchantId: options.merchantId
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getMerchantPartList();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    // 成员列表
    getMerchantPartList(){
        request({
            url: "/merchantMarketing/getMerchantPartList",
            reqRSAENCRY: true, // 参数是否加密
            resRSADECRY: true, // 返回值是否解密
            data: {
                merchantId: this.data.merchantId,
                merchantType: this.data.merchantType,
            }
        }).then(res => {
            if(res.code == 0){
                this.setData({
                    memberList: res.data
                })
            }
        }).catch(error => {
            // console.log(error)
        })
    },

    /** 跳转添加成员 */
    toAddMember(e){
        wx.navigateTo({
          url: `/manager/pages/addmerchantmember/index?merchantId=${this.data.merchantId}`,
        })
    },

    /** 跳转成员详情 */
    toMemberDetail(e){
        let { id } = e.currentTarget.dataset;
        wx.navigateTo({
          url: `/manager/pages/merchantmemberdesc/index?merchantId=${this.data.merchantId}&memberId=${id}`,
        })
    },

    /**
     * 跳转商户预授信详情
     */
    toMerchantcreditdesc(e) {
        let { creditid } = e.currentTarget.dataset;
        wx.navigateTo({
          url: `/pages/merchantcreditdesc/index?id=${creditid}`,
        })
    },
    /** 设为联系人 */
    setMember(e){
        let { id } = e.currentTarget.dataset;
        let that = this;
        wx.showModal({
            title: '提示',
            content: `确认将该成员设为联系人？`,
            success (res) {
                if (res.confirm) {
                    request({
                        url: `/merchantMarketing/modifyMerchantContacts`,
                        reqRSAENCRY: true, // 参数是否加密
                        resRSADECRY: false, // 返回值是否解密
                        data: {
                            merchantId: that.data.merchantId,
                            merchantType: this.data.merchantType,
                            id
                        }
                    }).then(res => {
                        if(res.code == 0){
                            wx.showToast({
                                title: '设为成功',
                                icon: 'none',
                                mask: true,
                                duration: 2000
                            });
                        }
                    }).catch(error => {
                        // console.log(error)
                    })
                    // console.log('用户点击确定')
                } else if (res.cancel) {
                    // console.log('用户点击取消')
                }
            }
        })
    },
    /** 删除成员 */
    deleteMember(e){
        let { id } = e.currentTarget.dataset;
        let that = this;
        wx.showModal({
            title: '提示',
            content: `确认删除该成员？`,
            success (res) {
                if (res.confirm) {
                    request({
                        url: `/merchantMarketing/delectMerchant`,
                        reqRSAENCRY: true, // 参数是否加密
                        resRSADECRY: false, // 返回值是否解密
                        data: {
                            id,
                            merchantType: this.data.merchantType,
                        }
                    }).then(res => {
                        if(res.code == 0){
                            wx.showToast({
                                title: '删除成功',
                                icon: 'none',
                                mask: true,
                                duration: 2000
                            });
                            setTimeout(()=>{
                                that.getMerchantPartList();
                            },2000)
                        }
                    }).catch(error => {
                        // console.log(error)
                    })
                    // console.log('用户点击确定')
                } else if (res.cancel) {
                    // console.log('用户点击取消')
                }
            }
        })
    }
    
})